<?php
$SITEROOT = '../';
include_once($SITEROOT . 'assets/snippets/shoppingCart/dbconfig.inc.php');
include_once($SITEROOT . 'assets/snippets/shoppingCart/kiwee.class.php');

$kiwee = new Kiwee();

if(IN_MANAGER_MODE!="true") die('<b>' . $_lang['kiwee_include_order_error'] . '</b>');
if ($_SESSION['mgrPermissions']['settings'] != "1") { echo 'Insufficient permissions for this module.'; exit; }

$theme = $manager_theme ? "$manager_theme/":"";
$modx->manager->initPageViewState();

// get and save search string
if($_REQUEST['op']=='reset') {
	$sqlQuery = $query = '';
	$_PAGE['vs']['search']='';
} else {
	$sqlQuery = $query = isset($_REQUEST['search'])? $_REQUEST['search']:$_PAGE['vs']['search'];
	if(!is_numeric($sqlQuery)) $sqlQuery = mysql_escape_string($query);
	$_PAGE['vs']['search'] = $query;
}

// get & save listmode
$listmode = isset($_REQUEST['listmode']) ? $_REQUEST['listmode']:$_PAGE['vs']['lm'];
$_PAGE['vs']['lm'] = $listmode;
  
// context menu
include_once $base_path . "manager/includes/controls/contextmenu.php";
$cm = new ContextMenu("cntxm", 150);
$cm->addItem($_lang['view_log'],"js:menuAction(1)","media/style/" . ($manager_theme ? "$manager_theme/":"") ."images/icons/save.gif");
$cm->addSeparator();
$cm->addItem($_lang['delete'], "js:menuAction(2)","media/style/" . ($manager_theme ? "$manager_theme/":"") ."images/icons/delete.gif",(!$modx->hasPermission('delete_eventlog') ? 1:0));
echo $cm->render();
?>
<div id="overlay" onclick="hideBox()" style="display:none"></div>
	
	<div id="box" style="display:none">
	    <img id="close" src="js/modal_window/images/close.gif" onclick="hideBox()" alt="Close"
	        title="Close this Window" />
	    <div id="title" style="color: #ff9933; font-weight: bold; font-size: 14px;">
			Customer Account Changes
	    </div>
	    <br />
	    <div id="inner-box">
	    </div>
</div>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/modal_window/javascripts/modal.js"></script>
<script type="text/javascript" src="actions/includes/interface.js"></script>
<script type="text/javascript" src="actions/includes/md5.js"></script>
<link href="js/modal_window/stylesheets/modal.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    function searchResource(){
        document.resource.op.value="srch";
        document.resource.submit();
    };

    function edituser(id){
	        var poststr = "trxntype=customers_properties&custid=" + id;
	        makePOSTRequest('actions/ajax.php', poststr, 'customers_properties');
    }
	
	    function passvalidator(){
	        if (document.getElementById('editpassword').value != document.getElementById('editpasswordconfirm').value){
	           document.getElementById('passvalidatediv').innerHTML='<p style=\"color: red;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Passwords don\'t match</p>';
	           document.getElementById('passvalidatediv').show();;
	           document.getElementById('updateform').hide();
	        } else {
	           if (document.getElementById('editpassword').value.length >= 7){
	              document.getElementById('passvalidatediv').hide();
	              document.getElementById('updateform').show();
	           } else {
	              document.getElementById('passvalidatediv').innerHTML='<p style="color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;There is a 7 character minimum password length.</p>';
	              document.getElementById('passvalidatediv').show();
	              document.getElementById('updateform').hide();
	           }
	        }
	    }
	
	function resetSearch(){
        document.resource.search.value = ''
        document.resource.op.value="reset";
        document.resource.submit();
    };

    function changeListMode(){
        var m = parseInt(document.resource.listmode.value) ? 1:0;
        if (m) document.resource.listmode.value=0;
        else document.resource.listmode.value=1;
        document.resource.submit();
    };

    var selectedItem;
    var contextm = <?php echo $cm->getClientScriptObject(); ?>;
    function showContentMenu(id,e){
        selectedItem=id;
        contextm.style.left = (e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)))+"px";
        contextm.style.top = (e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)))+"px";
        contextm.style.visibility = "visible";
        e.cancelBubble=true;
        return false;
    };

    function menuAction(a) {
        var id = selectedItem;
        switch(a) {
            case 1:     // view log details
                window.location.href='index.php?a=115&id='+id;
                break;
            case 2:     // clear log
                window.location.href='index.php?a=116&id='+id ;
                break;
        }
    }

    document.addEvent('click', function(){
        contextm.style.visibility = "hidden";
    });
	
    function searchforcust(){
		var poststr = "trxntype=customers_gridload&custsearch=" + encodeURI(document.getElementById('custidtosearch').value);
		makePOSTRequest('actions/ajax.php', poststr, 'customers_gridload');
	}
	
	function updateuser(){
		var poststr = "trxntype=customers_update&editid=" + encodeURI(document.getElementById('editid').value) +
	    				"&editinternalkey=" + encodeURI(document.getElementById('editinternalkey').value) +
	                    "&pass=" + encodeURI(hex_md5(document.getElementById('editpassword').value)) +
	                    "&companyname=" + encodeURI(document.getElementById('editcompanyname').value) +
	                    "&username=" + encodeURI(document.getElementById('editusername').value) +
	                    "&fname=" + encodeURI(document.getElementById('editfname').value) +
	                    "&lname=" + encodeURI(document.getElementById('editlname').value) +
	                    "&addr1=" + encodeURI(document.getElementById('editaddr1').value) +
	                    "&addr2=" + encodeURI(document.getElementById('editaddr2').value) +
	                    "&city=" + encodeURI(document.getElementById('editcity').value) +
	                    "&state=" + encodeURI(document.getElementById('editstate').value) +
	                    "&zip=" + encodeURI(document.getElementById('editzip').value) +
	                    "&phone_1=" + encodeURI(document.getElementById('editphone_1').value) +
	                    "&phone_2=" + encodeURI(document.getElementById('editphone_2').value) +
	                    "&ext_1=" + encodeURI(document.getElementById('editext_1').value) +
	                    "&ext_2=" + encodeURI(document.getElementById('editext_2').value) +
	                    "&fax=" + encodeURI(document.getElementById('editfax').value) +
	                    "&logincount=" + encodeURI(document.getElementById('editlogincount').value) +
	                    "&failedlogincount=" + encodeURI(document.getElementById('editfailedlogincount').value) +
	                    "&blocked=" + encodeURI(document.getElementById('editblocked').value) +
	                    "&commentsection=" + encodeURI(document.getElementById('editcommentsection').value) +
	                    "&blockeduntil=" + encodeURI(document.getElementById('editblockeduntil').value);
	        makePOSTRequest('actions/ajax.php', poststr, 'customers_update');
	}
	
	function ajaxrendercomponent(name) {
		if (name == 'customers_properties'){ 
			document.getElementById('inner-box').innerHTML=http_request.responseText; 
			showBox(); 
			document.getElementById('box').style.width='500px'; 
		} else if (name == 'customers_update'){ 
			hideBox(); 
			searchforcust(); 
		} else if (name == 'customers_gridload'){ 
			document.getElementById('customermaintenancegrid').innerHTML=http_request.responseText; 
		} else {
			alert(name);
		}
	}
</script>
<link href="actions/includes/kiwee.css" rel="stylesheet" type="text/css" />
<div class="kiwee-msc" id="mascot"><a href="http://www.kiweecommerce.org"><img src="actions/includes/Kiwee-small.gif" /></a></div>		
<div class="subTitle">
<span class="right"></span>
</div>
<div class="sectionHeader"><?php echo $_lang['cust_box_header']; ?></div>
<div class="sectionBody">
	<p><?php echo $_lang['cust_box_detail']; ?></p>
    <div class="searchbar">
		<div><?php echo $_lang['cust_searchfor']; ?></div>
		<div><input type="text" onchange="searchforcust()" id="custidtosearch"></div>
		<div><a href="#" title="<?php echo $_lang['search'];?>" onclick="searchforcust()"><?php echo $_lang['go']; ?></a></div>
		<div><a href="#" title="<?php echo $_lang['reset'];?>" onclick="document.getElementById('custidtosearch').value='';searchforcust();">
			<img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/refresh.gif" width="16" height="16"/></a></div>
	    <!--<div><a href="#" title="<?php echo $_lang['list_mode'];?>" onclick="changeListMode();return false;">
				<img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/table.gif" width="16" height="16"/></a></div>-->
    </div>
<div>
<br />
<div id="customermaintenancegrid">
<?php
	include_once $base_path . 'manager/includes/controls/datagrid.class.php';
	$sql = 'SELECT * from ' . $kiwee->config['fpx'] . 'customers';
	mysql_select_db($dbname);
	$ds = mysql_query($sql) or die ("MYSQL: " . mysql_error());
	$grd = new DataGrid('',$ds, $number_of_results);
	$grd->noRecordMsg = $_lang['no_records_found'];
	$grd->cssClass='grid';
	$grd->columnHeaderClass='gridHeader';
	$grd->itemClass='gridItem';
	$grd->altItemClass='gridAltItem';
	$grd->fields='id, lname, fname, companyname, lastorder, orderct, totalsales';
	$grd->columns= $_lang['cust_id']. ', ' . $_lang['cust_lname'] . "," . $_lang['cust_fname'] .  ", " . $_lang['cust_company'] . ", " . $_lang['cust_lastorder'] . ", " . $_lang['cust_orderct'] . ", " . $_lang['cust_totalorders'] . ", " . $_lang['cust_edit']; 
	$grd->colWidths="34,75,75,200,75,34,34,50,50";
	$grd->colAligns="left, left, left, left, center, center, left, center";
	$grd->colTypes=",,,,,,currency,template:<button id=\"edititem\" onclick=\"edituser([+id+])\">Edit</button>";
	if($listmode=='1') $grd->pageSize=0;
	if($_REQUEST['op']=='reset') $grd->pageNumber = 1;
	echo $grd->render();
?>
</div>
</div>
	
